#include "roletablewidget.h"
#include "QHeaderView"
#include "QFont"
#include "QDebug"
#include "QPushButton"
#include "QSharedDataPointer"
#include "QJsonObject"
#include "QJsonArray"
#include "QJsonDocument"
#include "QFile"
#include "QJsonDocument"
#include "QJsonValue"
#include "utils/deviceutils.h"
#include "setting.h"

RoleTableWidget::RoleTableWidget(QWidget *parent) : QTableWidget(parent)
{
    this->parent = parent;
}

RoleTableWidget::~RoleTableWidget()
{

}

void RoleTableWidget::init(){
    QStringList list;
    list<< "序号" <<"游戏帐号" << "游戏密码" << "角色索引" << "操作";

    QStringList tip;
    tip<< "序号" <<"游戏的帐号(必填)" << "游戏的密码(必填)" << "游戏中选择角色界面的第几个角色(必填)" << "操作";
    setHorizontalHeaderLabels(list);
    setFocusPolicy(Qt::FocusPolicy::NoFocus);
    QHeaderView *headerView = horizontalHeader();
    headerView->setSectionResizeMode(QHeaderView::Stretch); //自适应列宽
    headerView->setMinimumHeight(40);
    headerView->setStyleSheet("QHeaderView::section {background-color: #ededed; border-bottom:1px solid #000000}");
    headerView->setSelectionBehavior(QAbstractItemView::SelectionBehavior::SelectRows);
    headerView->setHighlightSections(false);

    for(int i = 0 ; i < list.size(); i++){
        QTableWidgetItem *item = new QTableWidgetItem(list.at(i));
        QFont font = this->font();
        font.setBold(true);
        item->setFont(font);
        item->setFlags(Qt::ItemFlag::NoItemFlags);
        item->setSelected(false);
        item->setBackground(Qt::gray);
        item->setToolTip(tip.at(i));
        if(i != 0 && i != list.size() -1){
            item->setForeground(Qt::red);
        }
        setHorizontalHeaderItem(i, item);

    }
    QList<RoleBean> roleBeans = readRolesByFile();
    if(!roleBeans.isEmpty()){
        for(RoleBean rolebean : roleBeans){
            creatNewRow( rolebean.account, rolebean.password, rolebean.roleIndex);
        }
    }
}

void RoleTableWidget::creatNewRow(QString account, QString password, int roleIndex)
{
    if(!isCreateRoleFinish())return;
    int row = rowCount();
    setRowCount(row  + 1);


    //序号
    QTableWidgetItem *item111 = new QTableWidgetItem();
    item111->setTextAlignment(Qt::AlignCenter);
    item111->setSelected(false);
    item111->setFlags(Qt::ItemFlag::NoItemFlags);
    item111->setText(QString::number(row + 1));
    this->setItem(row ,numType, item111);

    //游戏帐号
    QTableWidgetItem *item0 = new QTableWidgetItem();
    item0->setTextAlignment(Qt::AlignCenter);
    item0->setSelected(false);
    item0->setText(account);
    this->setItem(row ,accountType, item0);


    //游戏密码
    QTableWidgetItem *item11 = new QTableWidgetItem();
    item11->setTextAlignment(Qt::AlignCenter);
    item11->setSelected(false);
    item11->setText(password);
    this->setItem(row ,passwordType, item11);


    //帐号account
    QTableWidgetItem *item = new QTableWidgetItem();
    item->setTextAlignment(Qt::AlignCenter);
    item->setSelected(false);
    item->setText(roleIndex == 0 ? "": QString::number(roleIndex));
    this->setItem(row ,roleNameType, item);

    //操作 删除
    QPushButton *deleteButton = new QPushButton(this);
    deleteButton->setText("删除");
    deleteButton->setStyleSheet("color:red");
    setCellWidget(row, deleteType, deleteButton);
    connect(deleteButton, &QPushButton::clicked,this,[&](){
        this->removeRow(currentRow());
    }); // 再连接信号
}

bool RoleTableWidget::isCreateRoleFinish(){
    int row = rowCount();
    if(row == 0) return true;
    bool isFinish = true;
    for(int i = 0 ; i< columnCount() - 1; i++){
        QString content = item(row - 1,i)->text();
        isFinish = isFinish && !content.isEmpty();
    }
    return isFinish;
}

bool RoleTableWidget::getRoleBean(int row, RoleBean *roleBean){

    QString account = item(row ,accountType)->text();
    roleBean->account = account;

    QString password = item(row ,passwordType)->text();
    roleBean->password = password;

    bool isOk = false;
    int roleIndex= item(row ,roleNameType)->text().toInt(&isOk);
    if(roleIndex <= 0 || roleIndex >3) isOk = false;
    if(!isOk){
        emit logEvent("角色索引设置错误，范围1-3");
        return false;
    }

    roleBean->roleIndex = roleIndex;
    return true;
}

bool RoleTableWidget::getAllRoleBeans(QList<RoleBean*> *allRoleBeans)
{
    qDeleteAll(allRoleBeans->begin(), allRoleBeans->end());
    allRoleBeans->clear();
    for(int i = 0 ; i < rowCount() ; i++){
        RoleBean *roleBean = new RoleBean();
        if(getRoleBean(i, roleBean)){
            allRoleBeans->append(roleBean);
        }else{
            return false;
        }
    }
    if(allRoleBeans->isEmpty()){
        return false;
    }
    return true;
}

void RoleTableWidget::setData(QList<RoleBean> &roleBeans){
    for(int i = 0; i < roleBeans.size(); i++){
        RoleBean roleBean = roleBeans.at(i);
        creatNewRow(roleBean.account, roleBean.password, roleBean.roleIndex);
    }
}

bool RoleTableWidget::saveRoles2File(){
    QList<RoleBean *> allRoleBeans;
    if(getAllRoleBeans(&allRoleBeans)){
        QByteArray a= RoleBean::ArraytoJson(&allRoleBeans);
        Setting::getInstance()->setRoleBeanJson(QString(a));
        return true;
    }
    qDeleteAll(allRoleBeans.begin(), allRoleBeans.end());
    return false;

}

QList<RoleBean> RoleTableWidget::readRolesByFile()
{
    QString json = Setting::getInstance()->getRoleBeanJson();
    return RoleBean::jsonToArray(json.toUtf8());
}
